package com.lizi.apidoc.handler;

import com.lizi.apidoc.constants.DocGlobal;
import com.lizi.apidoc.model.ApiObjectProperty;
import com.lizi.apidoc.utils.DocClassUtil;
import com.lizi.apidoc.utils.JavaClassValidateUtil;
import com.lizi.apidoc.utils.StringUtil;
import com.thoughtworks.qdox.model.JavaClass;
import com.lizi.apidoc.builder.ClassLoader;

/**
 * Param Handler
 *
 * @author kingman zhangjw@3vjia.com
 * @since 2020-05-07 12:41
 */
public class ParamHandler {

    /**
     * 得到参数信息
     * @param paramName
     * @param fullTypeName
     * @param simpleName
     * @param typeName
     * @param comment
     * @param required
     * @return
     */
    protected ApiObjectProperty getApiParams(String paramName, String fullTypeName, String simpleName, String typeName, String comment, boolean required)
    {
        ApiObjectProperty apiObjectProperty = ApiObjectProperty.of().setDescription(comment)
                .setName(paramName).setType(simpleName).setRequired(required)
                .setFieldFullClassName(fullTypeName)
                .setVersion(DocGlobal.DEFAULT_VERSION);

        fillProperty(apiObjectProperty,fullTypeName);
        return apiObjectProperty;
    }

    /**
     *
     * @param className
     * @return
     */
    private void fillProperty(ApiObjectProperty apiObjectProperty,String className)
    {
        String simpleName = DocClassUtil.getSimpleName(className);
        String[] globGicName = DocClassUtil.getSimpleGicName(className);
        JavaClass cls = ClassLoader.getInstance().getJavaClassByFullTypeName(simpleName);

        //基础类型
        if (JavaClassValidateUtil.isPrimitive(simpleName)) {
            apiObjectProperty.setFormat(DocClassUtil.processTypeNameForParams(simpleName));
            return ;
        }
        //Map
        if(JavaClassValidateUtil.isMap(simpleName)){
            if (globGicName.length == 2) {
                fillProperty(apiObjectProperty,globGicName[1]);
                return ;
            }
            return ;
        }
        //Object
        if (DocGlobal.JAVA_OBJECT_FULLY.equals(className)) {
            apiObjectProperty.setFormat("object").setType("any object").setDescription(DocGlobal.ANY_OBJECT_MSG)
                    .setRequired(false).setVersion(DocGlobal.DEFAULT_VERSION);
            return ;
        }
        //泛型
        if (JavaClassValidateUtil.isCollection(simpleName) || JavaClassValidateUtil.isArray(simpleName)) {
            if (!JavaClassValidateUtil.isCollection(globGicName[0])) {
                String gicName = globGicName[0];
                if (JavaClassValidateUtil.isArray(gicName)) {
                    gicName = gicName.substring(0, gicName.indexOf("["));
                }
                String type = DocClassUtil.processTypeNameForParams(simpleName);
                apiObjectProperty.setType(type);
                apiObjectProperty.setFormat(type);

                String items_format = DocClassUtil.processTypeNameForParams(gicName);

                ApiObjectProperty items = ApiObjectProperty.of()
                        .setName(items_format).setType(gicName)
                        .setFieldFullClassName(gicName)
                        .setVersion(DocGlobal.DEFAULT_VERSION);
                if(JavaClassValidateUtil.isPrimitive(gicName)){
                    items.setType("object");
                }
                apiObjectProperty.setItems(items);
                fillProperty(items,gicName);
                return ;
            }
            return;
        }

        String des = apiObjectProperty.getDescription();
        if(StringUtil.isNullOrEmpty(des) || DocGlobal.NO_COMMENTS_FOUND.equals(des)){
            JavaClass javaClass = ClassLoader.getInstance().getJavaClassByFullTypeName(className);
            if(javaClass!=null){
                des = javaClass.getComment();
            }
            if(StringUtil.isNullOrEmpty(des)){
                des = DocGlobal.NO_COMMENTS_FOUND;
            }
        }

        String ref = null;
        if(!StringUtil.isNullOrEmpty(className)){
            ref = String.format("#/components/schemas/%s",className);
        }
        apiObjectProperty.setFieldFullClassName(className).setFormat("object").setType("object").setRef(ref).setDescription(des);

    }


    protected String paramCommentResolve(String comment) {
        if (StringUtil.isNullOrEmpty(comment)) {
            comment = DocGlobal.NO_COMMENTS_FOUND;
        } else {
            if (comment.contains("|")) {
                comment = comment.substring(0, comment.indexOf("|"));
            }
        }
        return comment;
    }
}
